package com.zcn.io.web.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcn.io.common.exception.ServiceException;
import com.zcn.io.common.util.DateUtil;
import com.zcn.io.common.vo.OrderMainForTrusteeshipVo;
import com.zcn.io.web.api.dto.MaintenanceDto;
import com.zcn.io.web.api.entity.Maintenance;
import com.zcn.io.web.api.query.MaintenanceQuery;
import com.zcn.io.web.api.vo.MaintenanceVo;
import com.zcn.io.web.api.vo.TrusteeshipDetailVo;
import com.zcn.io.web.mapper.MaintenanceMapper;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;



@Service
@RequiredArgsConstructor
public class MaintenanceService {

    private final MaintenanceMapper maintenanceMapper;

    private final TrusteeshipDetailService trusteeshipDetailService;


    /**
     * @desc: 获取维护信息列表
     * @author 202438 邱建伟
     * @date 2025/4/20 14:46
     */
    public void list(Page<MaintenanceVo> page, MaintenanceQuery query) {
        this.maintenanceMapper.list(page, query);
    }

    /**
     * @desc: 从托管详情中指派一条维护信息
     * @author 202438 邱建伟
     * @date 2025/5/1 14:32
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public synchronized void addMaintenance(Maintenance maintenance) {
        if (StrUtil.isEmpty(maintenance.getTgDtlId())){
            throw new ServiceException("托管详情id不能为空");
        }
        //1.设置创建时间和处理状态
        String status = "进行中";
        maintenance.setStatus(status);
        maintenance.setCreateDateTime(DateUtil.getDefaultDate());
        maintenance.setLastHandleTime(DateUtil.getDefaultDate());
        //2.判断当前平台是否有未完成的维护订单如果有则不允许再次指派。
        boolean flag = this.hasUnfinishedMaintenance(maintenance.getTgDtlId(), status);
        if (flag) {
            throw new ServiceException("当前平台有未完成的维护订单，不允许再次指派!");
        }
        //4.保存维护信息
        maintenanceMapper.insert(maintenance);
        //3.将最新维护订单的信息更新到托管详情表中
        trusteeshipDetailService.updateMaintenanceLatestId(maintenance);
    }

    /**
     * @desc: 判断当前平台是否有未完成的维护订单
     * @author 202438 邱建伟
     * @date 2025/5/1 14:32
     */
    private boolean hasUnfinishedMaintenance(String tgDtlId, String status) {
        return this.maintenanceMapper.getUnfinishedMaintenanceNumber(tgDtlId, status) > 0;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateMaintenance(MaintenanceDto maintenance) {
        //1.更新维护表信息
        maintenance.setLastHandleTime(DateUtil.getDefaultDate());
        this.maintenanceMapper.updateById(maintenance);
        //2.更新托管详情表信息
        TrusteeshipDetailVo trusteeshipDetailVo = new TrusteeshipDetailVo();
        trusteeshipDetailVo.setId(maintenance.getTgDtlId());
        trusteeshipDetailVo.setPlatformType(maintenance.getPlatformType());
        trusteeshipDetailVo.setPlatform(maintenance.getPlatform());
        trusteeshipDetailVo.setTrusteeshipAmount(maintenance.getTrusteeshipAmount());
        this.trusteeshipDetailService.updateTrusteeshipDetail(trusteeshipDetailVo);
    }

    public OrderMainForTrusteeshipVo getTrusteeshipDetailById(String dtlId) {
        return this.maintenanceMapper.getTrusteeshipDetailById(dtlId);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteById(String id) {
        //1.现根据id查询维护信息
        Maintenance maintenance = this.maintenanceMapper.selectById(id);
        if (maintenance == null) {
            throw new ServiceException("维护信息不存在");
        }
        //2.先删除维护订单信息
        this.maintenanceMapper.deleteById(id);
        //3.将当前托管明细下最新的维护单id更新到托管明细表中
        this.trusteeshipDetailService.updateMaintenanceLatestIdByTgDtlId(maintenance.getTgDtlId());
    }
}
